Java continue Statement

Author: SAI K


Core Java | Java Tutorial

Introduction

The continue statement in Java is a control flow statement that is used to skip the current iteration of a loop and proceed to the next iteration. It is commonly used within for, while, and do-while loops to control the flow of the loop based on certain conditions.

Table of Contents

What is a continue Statement?

The continue statement is used to skip the remainder of the code in the current iteration of a loop and immediately proceed to the next iteration. This is particularly useful when you want to skip certain iterations based on specific conditions.

Syntax of continue Statement

continue;

How continue Statement Works

Simple continue Statement Example


public class SimpleContinueExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                continue;
            }
            System.out.println("i: " + i);
        }
    }
}

Explanation: This loop prints the numbers 1, 2, 4, and 5. When i equals 3, the continue statement skips the remainder of the loop body, so 3 is not printed.

continue Statement in a for Loop


public class ContinueInForLoop {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue;
            }
            System.out.println("Odd number: " + i);
        }
    }
} 

Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.

continue Statement in a while Loop


public class ContinueInWhileLoop {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 10) {
            if (i % 2 == 0) {
                i++;
                continue;
            }
            System.out.println("Odd number: " + i);
            i++;
        }
    }
}

Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.

continue Statement in a do-while Loop


public class ContinueInDoWhileLoop {
    public static void main(String[] args) {
        int i = 1;
        do {
            if (i % 2 == 0) {
                i++;
                continue;
            }
            System.out.println("Odd number: " + i);
            i++;
        } while (i <= 10);
    }
}

Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.

continue Statement in Nested Loops


public class ContinueInNestedLoops {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    continue;
                }
                System.out.println("i: " + i + ", j: " + j);
            }
        }
    }
}

Explanation: This loop skips the inner loop's iteration when j equals 2, so it does not print pairs where j is 2.

Using continue with Labels


public class ContinueWithLabel {
    public static void main(String[] args) {
        outerLoop: for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    continue outerLoop;
                }
                System.out.println("i: " + i + ", j: " + j);
            }
        }
    }
}

Explanation: This loop uses a label outerLoop and continues the outer loop when j equals 2, skipping to the next iteration of the outer loop.

Conclusion

The continue statement in Java is a useful control flow tool that allows you to skip the current iteration of a loop and proceed to the next iteration. Understanding how to use continue effectively, including within nested loops and with labels, can help you write more efficient and readable code.